﻿
Namespace Processing
    Public Class DeleteProcjectCache
        Inherits BaseProcess(Of DeleteProcjectCache)
        Implements Interfaces.IProcess

        Public Sub Action() Implements Interfaces.IProcess.Action
            CleanProcjectCache()
        End Sub

        Public ReadOnly Property Name As String Implements Interfaces.IProcess.Name
            Get
                Return "CleaningProject Cache"
            End Get
        End Property

        Protected Shared Sub CleanProcjectCache()
            DeleteProcject("8")
            DeleteProcject("9")
            DeleteProcject("10.0")
        End Sub

        Private Shared Sub DeleteDirectoryPass(ByVal rootDir As String)
            If My.Computer.FileSystem.DirectoryExists(rootDir) Then
                My.Computer.FileSystem.GetDirectories(rootDir).AsParallel.ForAll(Sub(deldir)
                                                                                     Try
                                                                                         My.Computer.FileSystem.DeleteDirectory(deldir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                                                                                     Catch ex As Exception
                                                                                         'Do nothing just skip
                                                                                     End Try
                                                                                 End Sub)
            End If
        End Sub

        Private Shared Sub DeleteProcject(ByVal version As String)
            Dim rootCacheDirectory As String
            Dim userCacheDirectory As String
            Dim myDocuments As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            If (Environment.OSVersion.Version.Major > 5) Then
                rootCacheDirectory = "{0}:\Users\{1}\AppData\Local\Microsoft\VisualStudio\{2}\ProjectAssemblies"
                userCacheDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Users\")).Replace("\Documents", "").Replace("Users\", "")
            Else
                rootCacheDirectory = "{0}:\Documents and Settings\{1}\Local Settings\Application Data\Microsoft\VisualStudio\{2}\ProjectAssemblies"
                userCacheDirectory = myDocuments.Remove(0, myDocuments.LastIndexOf("Documents and Settings\")).Replace("\My Documents", "").Replace("Documents and Settings\", "")
            End If
            Dim systemDrive As String = Environment.SystemDirectory.Substring(0, 1)
            Dim directory As String = String.Format(rootCacheDirectory, systemDrive, userCacheDirectory, version)
            If My.Computer.FileSystem.DirectoryExists(directory) Then
                For x As Integer = 1 To 4
                    DeleteDirectoryPass(directory)
                Next
            End If
        End Sub


    End Class
End Namespace

